//
//  LCGlobalVideoViewController.m
//  lechuang
//
//  Created by Mr_hi on 2024/8/23.
//  Copyright © 2024 Mooyi. All rights reserved.
//

#import "LCGlobalVideoViewController.h"
#import "LCGolbalVideoCollectionViewCell.h"
#import "LCGlobalService.h"

#import "LCGlobalVideoDetailViewController.h"
@interface LCGlobalVideoViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, LCCollectionWaterFlowLayoutDeletage>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (assign, nonatomic)NSInteger page;
@property (strong, nonatomic)NSMutableArray * dataArr;

@end

@implementation LCGlobalVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    LCCollectionWaterFlowLayout * layout = [[LCCollectionWaterFlowLayout alloc] init];
    layout.delegate = self;
    self.collectionView.collectionViewLayout = layout;
    
    [self setContentViewRefresh];
    [self getVideoData:1];
}

-(void)setContentViewRefresh {
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self getVideoData:1];
    }];
    self.collectionView.mj_header.automaticallyChangeAlpha = YES;
    
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self getVideoData:self.page];
    }];
    self.collectionView.ly_emptyView = [LCEmptyView emptyViewWithImage:nil titleStr:@"暂无数据～" detailStr:nil];
}

- (void)getVideoData:(NSInteger)page {
    [LCGlobalService getVideoListWithPage:page Block:^(BOOL isSuccess, NSArray<LCGlobalVideoModel *> *list, NSInteger total) {
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        if (isSuccess) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
            }
            [self.dataArr addObjectsFromArray:list];
            if (self.dataArr.count >= total) {
                [self.collectionView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.collectionView reloadData];
        } else {
            if (self.page != 1) {
                self.page--;
            }
        }
    }];
}

-(NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArr.count;
}


-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCGolbalVideoCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LCGolbalVideoCollectionViewCell" forIndexPath:indexPath];
    LCGlobalVideoModel * video = self.dataArr[indexPath.item];
    cell.video = video;
    return cell;
}



-(NSInteger)collectionView:(UICollectionView *)collectionView layout:(LCCollectionWaterFlowLayout *)collectionViewLayout columnNumberAtSection:(NSInteger)section {
    return 3;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(LCCollectionWaterFlowLayout *)collectionViewLayout heightForRowAtIndexPath:(NSIndexPath *)indexPath itemWidth:(CGFloat)itemWidth {
    return itemWidth/53 * 60 + 45;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(LCCollectionWaterFlowLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 17, 14, 17);
}

-(NSInteger)collectionView:(UICollectionView *)collectionView layout:(LCCollectionWaterFlowLayout *)collectionViewLayout lineSpacingForSectionAtIndex:(NSInteger)section {
    return 14;
}

- (CGFloat) collectionView:(UICollectionView *)collectionView layout:(LCCollectionWaterFlowLayout *)collectionViewLayout interitemSpacingForSectionAtIndex:(NSInteger)section {
    return 14;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LCGlobalVideoModel * video = self.dataArr[indexPath.item];
    
    NSMutableDictionary * param = [NSMutableDictionary dictionary];
    param[@"filmid"] = video.filmid;
    [LCGlobalService getVideoDetailWithParam:param Block:^(BOOL isSuccess, LCGlobalVideoModel *model) {
        if (isSuccess) {
            LCGlobalVideoDetailViewController * detailVC = [LCGlobalVideoDetailViewController xx_instantiateFromStoryboardNamed:@"Global"];
            detailVC.video = model;
            [self.navigationController pushViewController:detailVC animated:YES];
        }
    }];
    
    
}

@end
